package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.app.dao.BsBookDao;
import io.renren.modules.app.dao.BsBookcategoryDao;
import io.renren.modules.app.entity.BsBookEntity;
import io.renren.modules.app.entity.BsBookcategoryEntity;
import io.renren.modules.app.entity.CountBookNumVo;
import io.renren.modules.app.service.BsBookService;
import io.renren.modules.app.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;



@Service("bsBookService")
@Slf4j
public class BsBookServiceImpl extends ServiceImpl<BsBookDao, BsBookEntity> implements BsBookService {

    @Resource
    private BsBookcategoryDao bsBookcategoryDao ;

    @Resource
    private BsBookDao bsBookDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params, HttpServletRequest request) {

        LambdaQueryWrapper<BsBookEntity> queryWrapper = new LambdaQueryWrapper<>() ;
        if (params.get("bookCateid") != null && !params.get("bookCateid").equals("")){
            queryWrapper.eq(BsBookEntity::getBookcateid,params.get("bookCateid"));
        }
        if (params.get("bookname") != null && !params.get("bookname").equals("")){
            queryWrapper.like(BsBookEntity::getBookname,params.get("bookname"));
        }
        if (params.get("bookauthor") != null && !params.get("bookauthor").equals("")){
            queryWrapper.like(BsBookEntity::getBookauthor,params.get("bookauthor"));
        }
        if (params.get("bookpublish") != null && !params.get("bookpublish").equals("")){
            queryWrapper.like(BsBookEntity::getBookpublish,params.get("bookpublish"));
        }
        if (params.get("bookstock") != null && !params.get("bookstock").equals("")){
            queryWrapper.eq(BsBookEntity::getBookstock,params.get("bookstock"));
        }
        if (params.get("booklocation") != null && !params.get("booklocation").equals("")){
            queryWrapper.like(BsBookEntity::getBooklocation,params.get("booklocation"));
        }

        IPage<BsBookEntity> page = this.page(
                new Query<BsBookEntity>().getPage(params),
                queryWrapper
        );

        for (BsBookEntity record : page.getRecords()) {
            Integer bookcateid = record.getBookcateid();
            BsBookcategoryEntity bsBookcategoryEntity = bsBookcategoryDao.selectById(bookcateid);
            if (bsBookcategoryEntity != null){
                record.setCatetypename(bsBookcategoryEntity.getCatetypename());
            }
            //图书图片数据清洗
            if (StringUtils.isNotBlank(record.getBookphoto())){
                record.setBookphoto(record.getBookphoto()+ "?token=" + request.getHeader("token"));
            }
        }

        return new PageUtils(page);
    }

    @Override
    public List<BsBookcategoryEntity> getBookcatename() {
        return bsBookcategoryDao.selectList(null);
    }

    @Override
    public void saveBook(BsBookEntity bsBook) {
        try {
            //生成书籍编号
            StringBuffer s = new StringBuffer() ;
            s.append("0000").append(bsBook.getBookcateid()).append(new Date().getTime());
            bsBook.setBooknum(String.valueOf(s));
            bsBookDao.insert(bsBook);
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }

    @Override
    public String bookUpload(MultipartFile file) {
        String url= null;//将传递的图片交给FileUtil处理
        try {
            url = FileUtil.uploadimg(file);
            return url ;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BsBookEntity queryById(Integer id, HttpServletRequest request) {
        BsBookEntity bsBookEntity = bsBookDao.selectById(id);
        bsBookEntity.setBookphoto(bsBookEntity.getBookphoto()+ "?token=" + request.getHeader("token"));
        return bsBookEntity ;
    }

    @Override
    public void updateBookById(BsBookEntity bsBook) {
        if (StringUtils.isNotBlank(bsBook.getBookphoto())){
            String bookPhoto = bsBook.getBookphoto();
            if (bookPhoto.contains("?")) {
                String[] split = bookPhoto.split("\\?");
                bsBook.setBookphoto(split[0]);
            }
        }
        bsBookDao.updateById(bsBook);
    }

    @Override
    public List<CountBookNumVo> countBookNum() {
        return bsBookDao.countBookNum();
    }

    @Override
    public List<CountBookNumVo> countBookCert() {
        return bsBookDao.countBookCert();
    }

}
